﻿using System.Drawing;
using System.Drawing.Imaging;

namespace UnvApi
{
    public class PersonInfo
    {
        /// <summary>
        /// 宇视平台内置人员ID，新增时可为空，新增后生成，修改时不可为空
        /// </summary>
        public int? PersonID { get; set; }
        /// <summary>
        /// 姓名（必须）
        /// </summary>
        public string PersonName {  get; set; }
        /// <summary>
        /// 最近一次修改时间（秒级时间戳，默认当前操作时间）
        /// </summary>
        public long LastChange {  get; set; }
        /// <summary>
        /// 性别（0-未知；1-男；2-女）
        /// </summary>
        public int Gender { get; set; }
        /// <summary>
        /// 生日
        /// </summary>
        public string Birthday {  get; set; }
        /// <summary>
        /// 电话
        /// </summary>
        public string Telephone { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 要录入的证件个数，默认、至少为1
        /// </summary>
        public int IdentificationNum {  get; set; }
        /// <summary>
        /// 证件信息
        /// </summary>
        public struct Identification
        {
            /// <summary>
            /// 证件类型（0-身份证；1-ic卡；2-护照）
            /// </summary>
            public int Type { get; set; }
            /// <summary>
            /// 证件号码
            /// </summary>
            public string Number { get; set; }
        }
        /// <summary>
        /// 证件信息列表（必须，至少写一个）
        /// </summary>
        public Identification[] IdentificationList {  get; set; }
        /// <summary>
        /// 所属人员库id，作用暂不明，默认0
        /// </summary>
        public int LibID {  get; set; }
        /// <summary>
        /// 不明作用的类型信息，默认0
        /// </summary>
        public int Type {  get; set; }
        /// <summary>
        /// 员工信息结构体
        /// </summary>
        public struct StaffInfo
        {
            /// <summary>
            /// 宇视系统内的外显序号（还有个内部id，第一次新增后系统生成并返回）
            /// </summary>
            public string Number { get; set; }
            /// <summary>
            /// 生日
            /// </summary>
            public string Birthday { get; set; }
            /// <summary>
            /// 部门id，通过getDepartments()获取，例如：3-总集合，23-宝信软件，34-宝粤气体，40-制氧，等等。
            /// </summary>
            public int DeptID { get; set; }
            /// <summary>
            /// 部门名称，通过getDepartments()获取，例如与23对应的"宝信软件"
            /// </summary>
            public string DeptName { get; set; }
        }
        /// <summary>
        /// 员工信息（必须）
        /// </summary>
        public StaffInfo Staff {  get; set; }
        /// <summary>
        /// 照片数量，可为0，若为0则不需要写后续的照片信息
        /// </summary>
        public int ImageNum {  get; set; }
        public struct ImageInfo
        {
            public string Name { get; set; }
            public long Size {  get; set; }
            public string Data { get; set; }
        }
        /// <summary>
        /// 照片信息列表，可为空
        /// </summary>
        public ImageInfo[]? ImageList {  get; set; }
        /// <summary>
        /// 作用未知参数，默认为空白字符串
        /// </summary>
        public string Desc {  get; set; }
        /// <summary>
        /// 最低基础信息构造函数，传入姓名&工号，部门分组使用默认的67-未定义分组人员，无照片信息
        /// </summary>
        /// <param name="personName">姓名</param>
        /// <param name="personId">工号</param>
        public PersonInfo(string personName, string personId)
        {
            PersonName = personName;
            LastChange = TimeFormatter.getCurrentTimestamp();
            Gender = 0;
            Birthday = "20240101";
            Telephone = "";//电话
            Address = "";//地址
            IdentificationNum = 1;//证件个数
            IdentificationList = new Identification[]//证件信息
            {
                new Identification
                {
                    Type = 0,
                    Number = personId
                }
            };
            LibID = 0;
            Type = 0;
            Staff = new StaffInfo
            {
                Number = personId,
                Birthday = "20240101",
                DeptID = 67,                //部门id，通过getDepartments()获取，例如：3-总集合，23-宝信软件，34-宝粤气体，40-制氧，等等。
                DeptName = "未定义分组人员"        //部门名称，同上
            };
            ImageNum = 0;
            Desc = "";
        }
        /// <summary>
        /// 含人脸照片路径的构造函数，部门分组使用默认的67-未定义分组人员
        /// </summary>
        /// <param name="personName">姓名</param>
        /// <param name="personId">工号</param>
        /// <param name="picPath">b本地人脸照片路径</param>
        public PersonInfo(string personName, string personId, string picPath) : this(personName, personId)
        {
            //读取到的图片大小（单位byte）
            long fileSizeInBytes = 0;
            //将图片转为Base64字符串
            string encodedImage = "";
            try
            {
                FileInfo fileInfo = new FileInfo(picPath);
                fileSizeInBytes = fileInfo.Length;

                using (var image = Image.FromFile(picPath))
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        // 将图片保存为指定的格式到内存流中  
                        image.Save(memoryStream, ImageFormat.Jpeg);
                        byte[] imageBytes = memoryStream.ToArray();

                        // 将字节数组转换为Base64字符串  
                        encodedImage = Convert.ToBase64String(imageBytes);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
            //如果读取到图片大小不为0
            if(fileSizeInBytes > 0)
            {
                ImageNum = 1;
                ImageList = new ImageInfo[]
                {
                    new ImageInfo
                    {
                        Name=picPath,
                        Size=fileSizeInBytes,
                        Data=encodedImage
                    }
                };
            }
        }
        /// <summary>
        /// 含人脸照片以外所有重要信息的构造函数
        /// </summary>
        /// <param name="personName">姓名</param>
        /// <param name="personId">工号</param>
        /// <param name="deptId">部门id，通过UnvVmsManager.getDepartments方法获取</param>
        /// <param name="deptName">部门名称，通过UnvVmsManager.getDepartments方法获取</param>
        public PersonInfo(string personName, string personId, int deptId, string deptName):this(personName, personId)
        {
            Staff = new StaffInfo
            {
                Number = personId,
                Birthday = "20240101",
                DeptID = deptId,
                DeptName = deptName
            };
        }
        /// <summary>
        /// 含所有重要信息的构造函数
        /// </summary>
        /// <param name="personName">姓名</param>
        /// <param name="personId">工号</param>
        /// <param name="deptId">部门id，通过UnvVmsManager.getDepartments方法获取</param>
        /// <param name="deptName">部门名称，通过UnvVmsManager.getDepartments方法获取</param>
        /// <param name="picPath">本地图片路径</param>
        public PersonInfo(string personName, string personId, int deptId, string deptName, string picPath):this(personName, personId, picPath)
        {
            Staff = new StaffInfo
            {
                Number = personId,
                Birthday = "20240101",
                DeptID = deptId,
                DeptName = deptName
            };
        }
    }
}
